<?php
include_once('ClsSebent/config.php');
$newfile=$_FILES['xfile']['name'];
$Uflag=0;
$dsq="select * from reseller where site_id=".$sid;
	$drs=mysql_query($dsq);	
	$drw=mysql_fetch_array($drs);
	$m_size=$drw['m_size'];
	$f_size=$drw['f_size'];
	$permission=$drw['p_uploads'];
	if($permission!='Y')
	{
	$Uflag=2;
	header('Location:page.php?site_id='.$sid.'&pid='.$pid.'&Uflag='.$Uflag);
	exit;
	}
if($newfile!="")
{
	//file upload function.
	$uploaddir="downloads_".$sid;
		
	if(!is_dir($uploaddir))
	mkdir($uploaddir, 777);
	
	//Check for Folder Size.
	$Mem=Dir_size($uploaddir);
	
	if(($Mem>=$m_size) && ($sid!=101))
	{
	$Uflag=3;
	header('Location:page.php?site_id='.$sid.'&pid='.$pid.'&Uflag='.$Uflag);
	exit;
	}	
	//check for File Size.
	if(($_FILES['xfile']['size'] > $f_size) && ($sid!=101))
	{
	$Uflag=4;
	header('Location:page.php?site_id='.$sid.'&pid='.$pid.'&Uflag='.$Uflag);
	exit;
	}
	//file upload function.	
	$uploaddir=$uploaddir."/";	
	$newfile=$_FILES['xfile']['name'];
	//$fnam=substr($newfile, 0, strrpos($newfile, "."));
	//echo $fnam.'<BR>';
	//$ext=substr($newfile, strrpos($newfile,".")+1, (strlen($newfile)-strrpos($newfile,".")));
	//echo $ext;
	//$dt=date("dS M Y");
	//$newfile=$fnam.'('.$dt.')pg.'.$ext;
	//echo $newfile;
			//print($newfile."<BR>");
			if(is_uploaded_file($_FILES['xfile']['tmp_name']))
			{
				move_uploaded_file($_FILES['xfile']['tmp_name'],$uploaddir.$newfile);
				$Uflag=1;
			}		
}
header('Location:page.php?site_id='.$sid.'&pid='.$pid.'&Uflag='.$Uflag);
?>

<?php
 function Dir_size($dir_name)
 {
$filesizex=0;
$hnd=opendir("./".$dir_name);
while($filex=readdir($hnd))
{	
	if($filex!="." && $filex!="..")
	{//echo "File: ".$filex." Size: ". (filesize("./tmp_upload/".$filex)/1024)."\n";
	$filesizex = $filesizex + filesize("./".$dir_name."/".$filex);
	clearstatcache();
	 }	
}
closedir($hnd);
//echo "Directory size: ". ($filesizex/1024);
return $filesizex;
 }
?>
